package com.jtalker.client.plugins;

/*
 * PlugInGUIONE.java
 *
 * Created on 25. November 2005, 10:51
 */

import java.lang.reflect.Method;

import com.jtalker.client.JTalkerClientCommandInterface;
import com.jtalker.client.JTalkerClientPlugInInterface;
import com.jtalker.client.JTalkerClientPluginConnector;


/**
 *
 * @author  chrmac@gmx.de
 */
public class PlugInGUIONE extends javax.swing.JFrame implements JTalkerClientPlugInInterface, JTalkerClientCommandInterface {
    
    // The parent translator object
    private JTalkerClientPluginConnector myParentObject = null;
    
    /** Creates new form PlugInGUIONE */
    public PlugInGUIONE() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setText("Plug In \"ONE\" Here!");
        jScrollPane1.setViewportView(jTextArea1);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

	ClassLoader cl = ResourceAnchor.class.getClassLoader();
	this.myIcon = new javax.swing.ImageIcon(cl.getResource("images/my_icon.png"));

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /** Interface Methods */
    // Gets the main PlugIn Frame
    public javax.swing.JFrame getMainFrame() {
        jTextArea1.setText("Online: " + this.myParentObject.isClientOnline() + "\nUser Name: " + this.myParentObject.getUserName() +  "\nServer Name: " + this.myParentObject.getServerName() + "\nUsers online: " + this.myParentObject.getOnlineUsers());

        // Sending a command to myself
        if(this.myParentObject.isClientOnline()) {
            this.myParentObject.sendMessage("jtplugin.PlugInGUIONE::sayHello()", this.myParentObject.getUserName(), "command", "Nothing", "msgid007");
        }
        return this;
    }
    // Gets settings Window for this Plugin
    public javax.swing.JFrame getSettingsFrame() {
	    return null;
    }

    // Gets the plugin name
    public String getName() {
	    return "Dummy Plugin ONE";
    }

    // Gets the description
    public String getDescription() {
	    return "This is a dummy plugin for testing only";
    }

    // Gets an Icon for clients toolbar
    public javax.swing.ImageIcon getIcon() {
        return this.myIcon;
    }


    // Sets the parent object for accessing the client application
    public boolean setParentObject(JTalkerClientPluginConnector _object) {
	    this.myParentObject = _object;
            this.myParentObject.registerForCommandResponse(this);
	    System.out.println("+ Ok, parent object initialized: " + _object);
	    return true;
    }

    // Free all ressources
    public void unload() {
        this.myParentObject.deRegisterForCommandResponse(this);
        this.myParentObject = null;
    }

    /** Gets available methods in this PlugIn */
    public Method[] getCommandMethods() {
      Method [] myMethods = new Method[2];

      try {	 
	 myMethods[0] = 
	    this.getClass().getDeclaredMethod("getEcho", new Class [] {String.class});
	 myMethods[1] = 
	    this.getClass().getDeclaredMethod("sayHello");
      } catch (Exception _e) {
	 _e.printStackTrace();
	 return null;
      } 
      
      return myMethods;	 
    }

    //////////////////// Command Methods ////////////////////

    /** Gets an echo */
    public String getEcho(String _in) {      
        return "Echo: " + _in;
    }
    /** Gets a hello */
    public String sayHello() {      
        return "Hello from " + this.getName();
    }
    
    //////////////////// End Command Methods /////////////////
    
    /** Gets available user methods in this PlugIn */
    public Method[] getUserMethods() {
      Method [] myMethods = new Method[1];

      try {	 
	 myMethods[0] = 
	    this.getClass().getDeclaredMethod("getUserInfo", new Class [] {String.class});
      } catch (Exception _e) {
	 _e.printStackTrace();
	 return null;
      } 
      
      return myMethods;	 
    }

    //////////////////// User Methods ////////////////////

    /** Gets info for given user */
    public String getUserInfo(String _username) {      
        return "User: " + _username;
    }
    
    //////////////////// End User Methods /////////////////
    
    /** A command answer will be send to us from the server using this method 
     *  @return true, if the _id belongs to our command */
    public boolean setCommandAnswer(String _id, String _from, String _content) {
        if(_id.equals("msgid007")) {
            jTextArea1.setText(jTextArea1.getText() + "\nAnswer: " + _content + ", From: " + _from);
            return true;
        }

        return false;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PlugInGUIONE().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.ImageIcon myIcon = null;
    // End of variables declaration//GEN-END:variables
    
}
